package bb.lanxing.view;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeOption;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;

import bb.lanxing.R;
import bb.lanxing.activity.PhotoViewerActivity;
import bb.lanxing.model.data.IBasePOI;
import bb.lanxing.util.BiCiCoorConverter;
import bb.lanxing.util.DensityUtil;
import bb.lanxing.util.Log;
import bb.lanxing.util.image.GlideUtils;

public class MarkerInfoView extends LinearLayout {
    final static private String TAG = "MarkerInfoView";
    public static final int STATE_SMALL = 1;
    public static final int STATE_BIG = 2;
    public static final int STATE_HIDE = 3;
    private TextView addressView;
    private float bigStateHeight;
    private Context context;
    private TextView descriptionView;
    private LinearLayout detailContainer;
    private ImageView dragView;
    private GeoCoder geoCoder;
    private String[] imageArray;
    private LinearLayout imageContainer;
    private IBasePOI infoPoint;
    private float lastTouchY;
    private View mDragView;
    private float smallStateHeight;
    private int state;
    private TextView titleView;

    public void setAddress(String str) {
    }

    public MarkerInfoView(Context context) {
        super(context);
        this.state = 3;
        this.context = context;
        init();
    }

    public MarkerInfoView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.state = 3;
        this.context = context;
        init();
    }

    private void init() {
        bindView(LayoutInflater.from(this.context).inflate(R.layout.view_marker_info, this));
    }

    private void onDragClick() {
        show(this.infoPoint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        if (action == 0) {
            showMoreInfo();
            this.lastTouchY = motionEvent.getRawY();
        } else if (action != 1) {
            if (action == 2) {
                float rawY = motionEvent.getRawY() - this.lastTouchY;
                Log.d("onTouchEvent ACTION_MOVE offset = " + rawY);
                int i = this.state;
                if (i == 1) {
                    float f = -rawY;
                    if (f > 0.0f) {
                        float f2 = this.bigStateHeight;
                        float f3 = this.smallStateHeight;
                        if (f <= f2 - f3) {
                            setTranslationY((f2 - f3) + rawY);
                        }
                    }
                } else if (i == 2 && rawY > 0.0f && rawY <= this.bigStateHeight - this.smallStateHeight) {
                    setTranslationY(rawY);
                }
            }
        } else if (getTranslationY() > (this.bigStateHeight - this.smallStateHeight) / 2.0f) {
            switchState(1);
        } else {
            switchState(2);
        }
        return true;
    }

    private void changeToSmall() {
        if (this.state == 3) {
            float f = this.smallStateHeight;
            Log.d("changeToSmall 1, startPosision = " + f + ", stopPosision = 0.0");
            ObjectAnimator ofFloat = ObjectAnimator.ofFloat(this, "translationY", f, 0.0f);
            ofFloat.setDuration(200L);
            ofFloat.setInterpolator(new DecelerateInterpolator());
            ofFloat.start();
            return;
        }
        float translationY = getTranslationY();
        float f2 = this.bigStateHeight - this.smallStateHeight;
        Log.d("changeToSmall 2, startPosision = " + translationY + ", stopPosision = " + f2);
        ObjectAnimator ofFloat2 = ObjectAnimator.ofFloat(this, "translationY", translationY, f2);
        ofFloat2.setDuration(200L);
        ofFloat2.setInterpolator(new DecelerateInterpolator());
        ofFloat2.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                MarkerInfoView.this.hideMoreInfo();
            }
        });
        ofFloat2.start();
    }

    private void changeToBig() {
        float translationY = getTranslationY();
        Log.d("changeToBig, startPosision = " + translationY + ", stopPosision = 0.0");
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(this, "translationY", translationY, 0.0f);
        ofFloat.setDuration(200L);
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationCancel(Animator animator) {
            }

            @Override
            public void onAnimationRepeat(Animator animator) {
            }

            @Override
            public void onAnimationStart(Animator animator) {
            }

            @Override
            public void onAnimationEnd(Animator animator) {
                MarkerInfoView.this.dragView.setImageResource(R.drawable.ic_marker_down);
            }
        });
        ofFloat.start();
    }

    public void changeToHide(float f) {
        Log.d("changeToHide, startPosision = 0.0, stopPosision = " + f);
        ObjectAnimator ofFloat = ObjectAnimator.ofFloat(this, "translationY", 0.0f, f);
        ofFloat.setDuration(200L);
        ofFloat.setInterpolator(new DecelerateInterpolator());
        ofFloat.start();
    }

    public void hideMoreInfo() {
        if (this.detailContainer.isShown()) {
            this.detailContainer.setVisibility(GONE);
            this.descriptionView.setSingleLine(true);
            this.dragView.setImageResource(R.drawable.ic_marker_up);
            setTranslationY(0.0f);
        }
        Log.d("hideMoreInfo, smallStateHeight = " + this.smallStateHeight);
    }

    private void showMoreInfo() {
        if (!this.detailContainer.isShown()) {
            this.detailContainer.setVisibility(VISIBLE);
            this.descriptionView.setSingleLine(false);
            float viewHeight = getViewHeight();
            this.bigStateHeight = viewHeight;
            setTranslationY(viewHeight - this.smallStateHeight);
        }
        Log.d("showMoreInfo, bigStateHeight = " + this.bigStateHeight);
    }

    public int getState() {
        return this.state;
    }

    public float getBigStateHeight() {
        return this.bigStateHeight;
    }

    public void hide() {
        int i = this.state;
        if (i == 1) {
            switchState(3);
        } else if (i == 2) {
            switchState(1);
        } else {
            setVisibility(GONE);
        }
    }

    public void show(IBasePOI iBasePOI) {
        if (iBasePOI == null) {
            return;
        }
        IBasePOI iBasePOI2 = this.infoPoint;
        if (iBasePOI2 != null && iBasePOI2.equals(iBasePOI)) {
            if (this.state == 1) {
                showMoreInfo();
                switchState(2);
                return;
            }
            setVisibility(VISIBLE);
            switchState(1);
            return;
        }
        initView(iBasePOI);
        this.infoPoint = iBasePOI;
        if (this.state != 3) {
            return;
        }
        if (this.smallStateHeight == 0.0f) {
            this.smallStateHeight = getViewHeight();
        }
        setVisibility(VISIBLE);
        switchState(1);
    }

    private void switchState(int i) {
        Log.d("switchState, this.state = " + this.state + ", state = " + i);
        if (i == 1) {
            changeToSmall();
        } else if (i == 2) {
            changeToBig();
        } else {
            changeToHide(this.smallStateHeight);
        }
        this.state = i;
    }

    private int getViewHeight() {
        measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int measuredHeight = getMeasuredHeight();
        Log.d("getViewHeight, measuredHeight = " + measuredHeight);
        return measuredHeight;
    }

    private void initView(IBasePOI iBasePOI) {
        this.titleView.setText(iBasePOI.getTitle());
        String content = iBasePOI.getContent();
        if (!TextUtils.isEmpty(content)) {
            this.descriptionView.setText(content);
            this.descriptionView.setVisibility(VISIBLE);
        } else {
            this.descriptionView.setText("");
            this.descriptionView.setVisibility(GONE);
        }
        if (!TextUtils.isEmpty(iBasePOI.getImage())) {
            this.imageArray = iBasePOI.getImage().split(";");
        } else {
            this.imageArray = null;
        }
        this.imageContainer.removeAllViews();
        String[] strArr = this.imageArray;
        if (strArr != null && strArr.length > 0) {
            int i = 0;
            while (true) {
                String[] strArr2 = this.imageArray;
                if (i >= strArr2.length) {
                    break;
                }
                String str = strArr2[i];
                ImageView imageView = new ImageView(this.context);
                imageView.setTag(i);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (MarkerInfoView.this.imageArray == null || MarkerInfoView.this.imageArray.length == 0) {
                            return;
                        }
                        Intent intent = new Intent(MarkerInfoView.this.context, PhotoViewerActivity.class);
                        intent.putExtra("cur_index", ((Integer) view.getTag()).intValue());
                        intent.putExtra("photo_url_array", MarkerInfoView.this.imageArray);
                        MarkerInfoView.this.context.startActivity(intent);
                    }
                });
                this.imageContainer.addView(imageView, new LayoutParams(DensityUtil.dp2px(100.0f), DensityUtil.dp2px(100.0f)));
                GlideUtils.companion.loadImage(imageView.getContext(), GlideUtils.companion.appendImagePath(str, 13), imageView, R.drawable.ic_default, R.drawable.ic_default);
                i++;
            }
            this.imageContainer.setVisibility(VISIBLE);
        } else {
            this.imageContainer.setVisibility(GONE);
        }
        initGeoCoder();
        reverseGeoCode(iBasePOI.getLatLng());
        bindView(this);
    }

    private void initGeoCoder() {
        GeoCoder newInstance = GeoCoder.newInstance();
        this.geoCoder = newInstance;
        newInstance.setOnGetGeoCodeResultListener(new OnGetGeoCoderResultListener() {
            @Override
            public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {
            }

            @Override
            public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
                final String address = reverseGeoCodeResult.getAddress();
                Log.d(TAG, "onGetReverseGeoCodeResult, address = " + address);
                MarkerInfoView.this.post(new Runnable() {
                    @Override
                    public void run() {
                        MarkerInfoView.this.addressView.setText(address);
                    }
                });
            }
        });
    }

    public void reverseGeoCode(LatLng latLng) {
        LatLng earth2Baidu = BiCiCoorConverter.earth2Baidu(latLng);
        ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
        reverseGeoCodeOption.location(earth2Baidu);
        this.geoCoder.reverseGeoCode(reverseGeoCodeOption);
    }

    private void bindView(View view) {
        this.dragView = (ImageView) view.findViewById(R.id.dragView);
        this.titleView = (TextView) view.findViewById(R.id.titleView);
        this.descriptionView = (TextView) view.findViewById(R.id.descriptionView);
        this.imageContainer = (LinearLayout) view.findViewById(R.id.imageContainer);
        this.addressView = (TextView) view.findViewById(R.id.addressView);
        this.detailContainer = (LinearLayout) view.findViewById(R.id.detailContainer);
        this.mDragView = view.findViewById(R.id.dragView);
        this.mDragView.setOnClickListener(view2 -> onDragClick());
    }

}